package it.yuzuojian.com.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.yuzuojian.com.domain.CommonResult;
import it.yuzuojian.com.domain.Singer;
import it.yuzuojian.com.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.BitSet;
import java.util.Date;
import java.util.List;
import java.util.Properties;

@RestController
@RequestMapping("/singer")
public class SingerController {
    @Autowired
    private SingerService singerService;
    @PostMapping("add")
    public CommonResult addSinger(Singer singer) throws ParseException {
        System.out.println("birth:"+singer.getBirth());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birth = singer.getBirth();
        singer.setBirth(dateFormat.parse(dateFormat.format(birth)));
        Boolean flag = singerService.insert(singer);
        if(flag) {
            return new CommonResult<Boolean>(1, "添加成功", true);
        }
        return new CommonResult<Boolean>(0, "添加失败", false);
    }
    @GetMapping("delete")
    public CommonResult deleteSinger(Integer id){
        Boolean flag = singerService.delete(id);
        System.out.println(flag);
        if(flag) {
            return new CommonResult<Boolean>(1, "删除成功", true);
        }
        return new CommonResult<Boolean>(0, "删除失败", false);
    }
    @PostMapping("update")
    public CommonResult updateSinger(@RequestBody String singerJson) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();

        Singer singer = objectMapper.readValue(singerJson, Singer.class);
        System.out.println(singer.toString());
        Boolean flag = singerService.update(singer);
        if(flag) {
            return new CommonResult<Boolean>(1, "修改成功", true);
        }
        return new CommonResult<Boolean>(0, "修改失败", false);
    }
    @GetMapping("findAll")
    public CommonResult findAll()  {
        List<Singer> singers = null;
        try {
            singers = singerService.selectAll();
            return new CommonResult<List<Singer>>(1, "查询成功", singers);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult<List<Singer>>(0, "查询失败", singers);
        }
    }
    @GetMapping("findById")
    public CommonResult findById(Integer id)  {
        Singer singer = null;
        try {
            singer = singerService.selectById(id);
            return new CommonResult<Singer>(1, "查询成功", singer);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult<Singer>(0, "查询失败", singer);
        }
    }
    @GetMapping("findByName")
    public CommonResult findByName(String name)  {
        List<Singer> singers = null;
        try {
            singers = singerService.selectByName(name);
            return new CommonResult<List<Singer>>(1, "查询成功", singers);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult<List<Singer>>(0, "查询失败", singers);
        }
    }
    @GetMapping("findBySex")
    public CommonResult findBySex(Integer sex)  {
        List<Singer> singers = null;
        try {
            singers = singerService.selectBySex(sex);
            return new CommonResult<List<Singer>>(1, "查询成功", singers);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult<List<Singer>>(0, "查询失败", singers);
        }
    }

    @PostMapping("/updateSingerPic")
    public CommonResult updateSingerPic(MultipartFile file, Integer id, HttpServletRequest request) throws IOException {
        if(file==null||file.isEmpty()){
            return new  CommonResult<Boolean>(0,"上传文件为空",false);
        }
        String fileName=System.currentTimeMillis()+file.getOriginalFilename();
        String path =System.getProperty("user.dir")+System.getProperty("file.separator")+"music"
                +System.getProperty("file.separator")+"img"+ System.getProperty("file.separator")
                +"singerPic"/*+System.getProperty("file.separator")*/;
        File file1=new File(path);
        if(file1.exists()){
            file1.mkdir();
        }
        File upload=new File(path,fileName);
        System.out.println("path = " + path);
        System.out.println("file:"+upload.getCanonicalPath());
        String pic = "/img/singerPic/"+fileName;
        try {
            file.transferTo(upload);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(pic);
            singerService.update(singer);
        } catch (IOException e) {
            e.printStackTrace();
            return new  CommonResult<Boolean>(0,"文件上传失败",false);
        }
        return new  CommonResult<Boolean>(1,"文件上传成功",true);

    }

}
